//
//  ZhuyeViewController.m
//  BlueTDevice
//
//  Created by MingmingSun on 16/9/8.
//  Copyright © 2016年 Sunmingming. All rights reserved.
//

#import "ZhuyeViewController.h"
#import <SVProgressHUD.h>
#import "ShebeiViewController.h"
#import "SMMNav.h"

@interface ZhuyeViewController ()<VCBlueTDelegate>

-(IBAction)searchPressed:(id)sender;

@property(nonatomic,weak) IBOutlet UILabel *mingchengLabel;
@property(nonatomic,weak) IBOutlet UILabel *zhuangtaiLabel;
@property(nonatomic,weak) IBOutlet UILabel *dianliangLabel;
@property(nonatomic,weak) IBOutlet UIButton *saomiaoButton;
@property(nonatomic,weak) IBOutlet UISegmentedControl *moshiSeg;

@property(nonatomic,weak) IBOutlet UIView *menuView;
@property(nonatomic,weak) IBOutlet UILabel *daojishiLabel;
@property(nonatomic,weak) IBOutlet UIButton *qiangdujianButton;
@property(nonatomic,weak) IBOutlet UIButton *qiangdujiaButton;
@property(nonatomic,weak) IBOutlet UILabel *qiangduLabel;

@end

@implementation ZhuyeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.moshiSeg addTarget:self action:@selector(segChanged:) forControlEvents:UIControlEventValueChanged];
    [self.qiangdujiaButton addTarget:self action:@selector(jiaPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.qiangdujianButton addTarget:self action:@selector(jianPressed:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    APPALL.myBlueTDelegate = self;
    if(!APPALL.currPeripheral){
        self.mingchengLabel.text = @"未连接设备";
        self.zhuangtaiLabel.hidden = YES;
        self.dianliangLabel.hidden = YES;
        [self.saomiaoButton setTitle:@"扫描设备" forState:UIControlStateNormal];
        self.moshiSeg.hidden = YES;
        self.menuView.hidden = YES;
    }else{
        self.mingchengLabel.text = [NSString stringWithFormat:@"设备名称:%@",APPALL.currPeripheral.name];
        self.zhuangtaiLabel.hidden = NO;
        self.dianliangLabel.hidden = NO;
        [self.saomiaoButton setTitle:@"重新扫描" forState:UIControlStateNormal];
        self.moshiSeg.hidden = NO;
        self.menuView.hidden = NO;
    }
}

-(IBAction)searchPressed:(id)sender{
    ShebeiViewController *vc = [ShebeiViewController new];
    SMMNav *nav = [[SMMNav alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)BlueTSucceed:(NSString*)aSucc{
    NSLog(@"zhuye S %@",aSucc);
    if([aSucc isEqualToString:@"Chatic"]){
        int jiechu = [tooles getIntFromData:APPALL.JiechuChatic.value];
        int moshi = [tooles getIntFromData:APPALL.MoshiChatic.value];
        int dangwei = [tooles getIntFromData:APPALL.DangweiChatic.value];
        int kaiguan = [tooles getIntFromData:APPALL.KaiguanChatic.value];
        int dianliang = [tooles getIntFromData:APPALL.DianliangChatic.value];
//        self.zhuangtaiLabel.text = [NSString stringWithFormat:@"状态:%@",jiechu?@"已接触":@"未接触"];
        self.zhuangtaiLabel.text = [NSString stringWithFormat:@"状态:%@",kaiguan == 5?@"运行":@"停止"];
        self.moshiSeg.selectedSegmentIndex = moshi - 1;
        self.qiangduLabel.text = [NSString stringWithFormat:@"%i",dangwei];
        self.dianliangLabel.text = [NSString stringWithFormat:@"剩余电量:%i%%",dianliang + 1];
        NSLog(@"writing into 2902");
        Byte cacheB = 0x0001;
        CBMutableCharacteristic *c = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:@"2902"] properties:CBCharacteristicPropertyWrite  value:[NSData dataWithBytes:&cacheB length:sizeof(cacheB)] permissions:CBAttributePermissionsReadable | CBAttributePermissionsWriteable];
        [APPALL.currPeripheral writeValue:[NSData dataWithBytes:&cacheB length:sizeof(cacheB)] forCharacteristic:c type:CBCharacteristicWriteWithResponse];
    }else if([aSucc isEqualToString:@"Dangwei"]){
        int dangwei = [tooles getIntFromData:APPALL.DangweiChatic.value];
        self.qiangduLabel.text = [NSString stringWithFormat:@"%i",dangwei];
    }
}

-(void)segChanged:(id)sender{
    Byte cacheB = (Byte)(self.moshiSeg.selectedSegmentIndex + 1);
    NSData *data = [NSData dataWithBytes:&cacheB length:sizeof(cacheB)];
    NSLog(@"%@",data);
    [APPALL writeToChatic:MoshiUUID withValue:data];
}

-(void)jiaPressed:(id)sender{
    int dangwei = [self.qiangduLabel.text intValue];
    if(dangwei >= 8)
        return;
    dangwei++;
    self.qiangduLabel.text = [NSString stringWithFormat:@"%d",dangwei];
    Byte cacheB = (Byte)dangwei;
    NSData *data = [NSData dataWithBytes:&cacheB length:sizeof(cacheB)];
    NSLog(@"%@",data);
    [APPALL writeToChatic:DangweiUUID withValue:data];
}

-(void)jianPressed:(id)sender{
    int dangwei = [self.qiangduLabel.text intValue];
    if(dangwei <= 1)
        return;
    dangwei--;
    self.qiangduLabel.text = [NSString stringWithFormat:@"%d",dangwei];
    Byte cacheB = (Byte)dangwei;
    NSData *data = [NSData dataWithBytes:&cacheB length:sizeof(cacheB)];
    NSLog(@"%@",data);
    [APPALL writeToChatic:DangweiUUID withValue:data];
}

- (void)BlueTFailed:(NSString*)aSucc{
    NSLog(@"zhuye F %@",aSucc);
}

@end
